package com.next.aappublicapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.next.aap.client.CandidateDto;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CandidateProfileActivity extends Activity {

	public static final String ARGS_CANDIDATE = "ARGS_CANDIDATE";
	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";
	
	private LinearLayout nameLayout;
	private LinearLayout educationLayout;
	private LinearLayout wealthLayout;
	private LinearLayout sourceOfIncomeLayout;
	private LinearLayout legalCasesLayout;
	private LinearLayout addressLayout;
	private LinearLayout contactNumber1Layout;
	private LinearLayout contactNumber2Layout;
	private LinearLayout objectivesLayout;
	private LinearLayout twitterHandleLayout;
	private LinearLayout facebookIdLayout;
	private LinearLayout profileLayout;
	
	private TextView nameTextView;
	private TextView educationTextView;
	private TextView wealthTextView;
	private TextView sourceOfIncomeTextView;
	private TextView legalCasesTextView;
	private TextView addressTextView;
	private TextView contactNumber1TextView;
	private TextView contactNumber2TextView;
	private TextView objectivesTextView;
	private TextView twitterHandleTextView;
	private TextView facebookIdTextView;
	private ImageView profileImageView;
	private WebView profileWebView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_candidate_profile);
		
		Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
		CandidateDto selectedCandidate = (CandidateDto) bundle.getSerializable(ARGS_CANDIDATE);
		
		nameTextView = (TextView)findViewById(R.id.candidateName);
		educationTextView = (TextView)findViewById(R.id.educationTextView);
		wealthTextView = (TextView)findViewById(R.id.wealthTextView);
		sourceOfIncomeTextView = (TextView)findViewById(R.id.sourceOfIncomeTextView);
		legalCasesTextView = (TextView)findViewById(R.id.legalCasesTextView);
		addressTextView = (TextView)findViewById(R.id.addressTextView);
		contactNumber1TextView = (TextView)findViewById(R.id.contactNumber1TextView);
		contactNumber2TextView = (TextView)findViewById(R.id.contactNumber2TextView);
		objectivesTextView = (TextView)findViewById(R.id.objectivesTextView);
		twitterHandleTextView = (TextView)findViewById(R.id.twitterHandleTextView);
		facebookIdTextView = (TextView)findViewById(R.id.facebookIdTextView);
		profileWebView = (WebView)findViewById(R.id.profileWebView);
		
		nameLayout = (LinearLayout)findViewById(R.id.nameLayout);
		educationLayout = (LinearLayout)findViewById(R.id.educationLayout);
		wealthLayout = (LinearLayout)findViewById(R.id.wealthLayout);
		sourceOfIncomeLayout = (LinearLayout)findViewById(R.id.sourceOfIncomeLayout);
		legalCasesLayout = (LinearLayout)findViewById(R.id.legalCasesLayout);
		addressLayout = (LinearLayout)findViewById(R.id.addressLayout);
		contactNumber1Layout = (LinearLayout)findViewById(R.id.contactNumber1Layout);
		contactNumber2Layout = (LinearLayout)findViewById(R.id.contactNumber2Layout);
		objectivesLayout = (LinearLayout)findViewById(R.id.objectivesLayout);
		twitterHandleLayout = (LinearLayout)findViewById(R.id.twitterHandleLayout);
		facebookIdLayout = (LinearLayout)findViewById(R.id.facebookIdLayout);
		profileLayout = (LinearLayout)findViewById(R.id.profileLayout);
		profileImageView = (ImageView)findViewById(R.id.profileImage);
		
		nameTextView.setText(selectedCandidate.getName());
		loadData(educationLayout, educationTextView, selectedCandidate.getEducation());
		loadData(wealthLayout, wealthTextView, selectedCandidate.getWealth());
		loadData(addressLayout, addressTextView, selectedCandidate.getAddress());
		loadData(contactNumber1Layout, contactNumber1TextView, selectedCandidate.getContactNumber1());
		loadData(contactNumber2Layout, contactNumber2TextView, selectedCandidate.getContactNumber2());
		loadData(facebookIdLayout, facebookIdTextView, selectedCandidate.getFacebookId());
		loadData(legalCasesLayout, legalCasesTextView, selectedCandidate.getLegalCases());
		loadData(objectivesLayout, objectivesTextView, selectedCandidate.getObjectives());
		loadData(twitterHandleLayout, twitterHandleTextView, selectedCandidate.getTwitterHandle());
		loadData(sourceOfIncomeLayout, sourceOfIncomeTextView, selectedCandidate.getSourceOfIncome());
		loadData(wealthLayout, wealthTextView, selectedCandidate.getWealth());
		Log.i("Ravi", selectedCandidate.getProfilePic());
		Log.i("Ravi", "profileImageView="+profileImageView);
		if(!isEmpty(selectedCandidate.getProfilePic())){
			Log.i("Ravi", "Displaying Image : "+selectedCandidate.getProfilePic());
			ImageLoader.getInstance().displayImage(selectedCandidate.getProfilePic(), profileImageView);
		}
		String profile = selectedCandidate.getProfile(); 
		if(!isEmpty(profile)){
			try{
				String base64 = android.util.Base64.encodeToString(profile.getBytes("UTF-8"), android.util.Base64.DEFAULT);
				profileWebView.loadData(profile, "text/html; charset=UTF-8", base64);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}

		

	}
	private void loadData(LinearLayout linearLayout,TextView textView,String data){
		if(isEmpty(data)){
			linearLayout.setVisibility(View.GONE);
		}else{
			linearLayout.setVisibility(View.VISIBLE);
			textView.setText(data);
		}
		
	}
	private boolean isEmpty(String str){
		if(str == null || str.trim().equals("")){
			return true;
		}
		return false;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.candidate_profile, menu);
		return true;
	}

}
